<?php
namespace app\member\controller;

use think\Controller;
use Db;
class Base extends Controller
{
    public function initialize()
    {
        $this->restLogin();
        $userid = session('userid');
        //define('UID', $userid); //设置登陆用户ID常量
        if(!isset($userid)){
            $this->error('重新登录', url('Login/auth'));
        }


        // define('MODULE_NAME', request()->module());
        // define('CONTROLLER_NAME', request()->controller());
        // define('ACTION_NAME', request()->action());
    }
    /**
     * 登录验证过程
     * @return [type] [description]
     */
    private function restLogin()
    {
        $userid = session('userid');
        if (empty($userid)) {
            //未登录
            $this->redirect('Login/loginOut');
        }
        //$config = new Config();
        $login_time = Db::name('webconfig')->where(['varname' => 'login_time'])->value('value');
        $now_token  = session('user_token'); //当前token

        $db_token = Db::name('token_user')->where(['uid' => $userid, 'type' => '1'])->find(); //数据库token
        if ($db_token['token'] != $now_token) {
            //其他地方登录
            $this->redirect('Login/loginOut');
        } else {
            if ($db_token['token_time'] < time()) {
                //登录超时
                //$this->error('错误', url('Login/index'));
                $this->redirect('Login/loginOut');
            } else {
                $token_time = time() + $login_time;
                $data       = ['token_time' => $token_time];
                Db::name('token_user')->where(['uid' => $userid, 'type' => '1'])->update($data);
            }
        }
        return;
    }





}
